Package xunome.game.multiplayerg

Source Code of xunome.game.multiplayerg.SocketConnectionClient

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package xunome.game.multiplayerg;

import java.io.InputStream;
import javax.microedition.io.Connector;
import xunome.xUnoME;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.SocketConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;

/**
*
* @author Sem iNick
*/
public class SocketConnectionClient implements Runnable{

    private String sessionName;
    private xUnoME mid;
    private String playerName;

    public SocketConnectionClient(String sessionName, String playerName, xUnoME mid) {

        this.mid = mid;
        this.playerName = playerName;
        this.sessionName = sessionName;
        new Thread(this).start();
    }

    public void run() {

        HttpConnection getHostCon = null;
        InputStream getRes = null;
        int result = 0;

        try {

            getHostCon = (HttpConnection) Connector.open(SocketConnectionServer.SERVER_SESSION
                                                        + "/multiplayer/getHost.php?session="
                                                        + sessionName);
            getRes = getHostCon.openInputStream();
            result = getRes.read();
            if (result == 0) {

                int ch;
                String url = "";
                while ((ch = getRes.read()) != -1) {

                    url += (char) ch;
                }

                SocketConnection connetion = (SocketConnection) Connector.open("socket://" + url);
                new MultiPlayerManager(mid, playerName).getDataListener()
                        .addConnetion(connetion);
            } else {

                throw new Exception("Error result.");
            }
        } catch (Exception e) {

            //#ifdef DEBUG
            e.printStackTrace();
            //#endif
            String errorMsg = "It wasn't possible to connect to server.";

            switch (result) {

                case 3:

                    errorMsg = "There was a error with server connection.";
                break;

                case 4:

                    errorMsg = "Session doesn't exist.";
                break;
            }
            Alert a = new Alert("ERROR:", errorMsg, null, AlertType.ERROR);
            a.setTimeout(3000);
            mid.getLCD().setCurrent(a);
        } finally {

            try {

                if (getRes != null) {

                    getRes.close();
                }
                if (getHostCon != null) {

                    getHostCon.close();
                }
            } catch (Exception e) {}
        }
    }
}
TOP

Related Classes of xunome.game.multiplayerg.SocketConnectionClient

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.